package com.ecoolex.hotel.saas.platform.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.Outlets;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddOutletsDTO;
import com.ecoolex.hotel.saas.pojo.EditOutletsDTO;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "营业点")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = OutletsGraphql.AUTH,
	name = "营业点管理",
	remark = "营业点管理")
public class OutletsGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-OUTLETS";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@Reference
	private StoreService service;
	
	@TypeField(type = Outlets.class, field = "departmentList", description = "关联的部门列表")
	public List<Department> departmentList(@Source Outlets source){
		return service.queryDepartmentByOutletsId(source.getOutletsId());
	}
	
	@QueryField(field = "outlets", description = { "营业点详情" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取营业点详情")
	public Outlets get(@NonNull Integer outletsId, @NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getOutlets(outletsId, storeId);
	}
	
	@QueryField(field = "outletsList", description = { "营业点列表" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "获取营业点列表")
	public Pagination<Outlets> query(String name,@NonNull Integer storeId, @NonNull Page page, PlatformMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return service.queryOutlets(name, storeId, page);
	}
	
	@MutationField(field = "addOutlets", description = "添加营业点")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加营业点")
	public Outlets add(@NonNull AddOutletsDTO add,@NonNull List<Integer> departmentIdList, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		Store store = service.getStore(add.getStoreId());
		Assert.isTrue(Check.notNull(store),ResultCode.VERIFY.build(1), "商户Id={}不存在", add.getStoreId());
		return service.addOutlets(add, departmentIdList, user.userName());
	}
	
	@MutationField(field = "editOutlets", description = "编辑营业点")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑营业点")
	public Outlets edit(@NonNull EditOutletsDTO edit,@NonNull List<Integer> departmentIdList, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		Outlets old = service.getOutlets(edit.getOutletsId(), edit.getStoreId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "营业点不存在");
		old = BeanUtil.copy(edit, old);
		return service.editOutlets(old, departmentIdList, user.userName());
	}
	
	@MutationField(field = "delOutlets", description = "删除营业点")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除营业点")
	public Boolean del(@NonNull Integer outletsId,@NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		Outlets old = service.getOutlets(outletsId, storeId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "营业点不存在");
		return service.delOutlets(old, user.userName());
	}
}
